import {
  getUserInfo,
  getRights
} from '@/api/user'
import { setToken, getToken } from '@/libs/util'

export default {
  state: {
    //用户信息
    info:{},
    //权限列表信息
    rights:[],
  },
  mutations: {
    setInfo(state,info){
      state.info = info;
    },
    setRights(state,rights){
      state.rights = rights;
    },
    setToken(state,token){
      setToken(token);
    }
  },
  getters: {    
  },
  actions: {    
    // 获取用户相关信息
    getUserInfo ({ state, commit }) {
      return new Promise((resolve, reject) => {
        try {
          getUserInfo().then(data => {
            commit('setInfo', data)
            resolve(data)
          }).catch(err => {
            reject(err)
          })
        } catch (error) {
          reject(error)
        }
      })
    },
    getRights({state,commit}){
      return new Promise((resolve,reject)=>{
          try {
            getRights().then(data=>{
              commit("setRights",data)
              resolve(data)
            }).catch(err=>{
              reject(err)
            })
          }catch(error){
            reject(error)
          }
      })
    }
  }
}
